package com.egan.pet.admin.controller;

import com.egan.entity.dto.LoginFormDTO;
import com.egan.entity.po.User;
import com.egan.pet.admin.result.Result;
import com.egan.pet.admin.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author egan
 * @since 2024-09-09
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户相关接口")
public class UserController {

    @Resource
    private IUserService userService;


    /**
     * 实现手机号登录
     * @param phone 手机号
     * @param session 存在session
     * @return
     */
    @Operation(summary = "用户发送验证码")
    @PostMapping("/code")
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session) {
        //发送验证码并保存验证码
        return Result.success(userService.sendCode(phone,session));
    }

    /**
     * 手机号登录
     * @param loginFormDTO
     * @param session
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户手机号登录")
    public Result login(@RequestBody LoginFormDTO loginFormDTO, HttpSession session) {
        //实现登录功能
        return Result.success(userService.login(loginFormDTO,session));
    }

    @PostMapping("/loginToPass")
    @Operation(summary = "用户密码登录")
    public Result loginToPass(@RequestBody LoginFormDTO loginFormDTO) {
        User user = userService.loginToPass(loginFormDTO);

        return Result.success(user);
    }
}
